package com.hopu.haogoumall.db.mapper;

import com.hopu.haogoumall.db.module.HaogoumallUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**@Mapper注解：定义此注解的功能在于能够让SpringBoot的主程序入口类中的@MapperScan能够找到该接口*/
@Mapper
public interface HaogoumallUserMapper {
    @Delete({
        "delete from haogoumall_user",
        "where user_id = #{userId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer userId);

    @Insert({
        "insert into haogoumall_user (user_id, user_username, ",
        "user_password, user_gender, ",
        "user_birthday, user_last_login_time, ",
        "user_last_login_ip, user_level, ",
        "user_nickname, user_mobile, ",
        "user_avatar, user_weixin_openid, ",
        "user_session_key, user_status, ",
        "user_add_time, user_update_time, ",
        "user_deleted)",
        "values (#{userId,jdbcType=INTEGER}, #{userUsername,jdbcType=VARCHAR}, ",
        "#{userPassword,jdbcType=VARCHAR}, #{userGender,jdbcType=TINYINT}, ",
        "#{userBirthday,jdbcType=DATE}, #{userLastLoginTime,jdbcType=TIMESTAMP}, ",
        "#{userLastLoginIp,jdbcType=VARCHAR}, #{userLevel,jdbcType=TINYINT}, ",
        "#{userNickname,jdbcType=VARCHAR}, #{userMobile,jdbcType=VARCHAR}, ",
        "#{userAvatar,jdbcType=VARCHAR}, #{userWeixinOpenid,jdbcType=VARCHAR}, ",
        "#{userSessionKey,jdbcType=VARCHAR}, #{userStatus,jdbcType=TINYINT}, ",
        "#{userAddTime,jdbcType=TIMESTAMP}, #{userUpdateTime,jdbcType=TIMESTAMP}, ",
        "#{userDeleted,jdbcType=BIT})"
    })
    int insert(HaogoumallUser record);

    @InsertProvider(type=HaogoumallUserSqlProvider.class, method="insertSelective")
    int insertSelective(HaogoumallUser record);

    @Select({
        "select",
        "user_id, user_username, user_password, user_gender, user_birthday, user_last_login_time, ",
        "user_last_login_ip, user_level, user_nickname, user_mobile, user_avatar, user_weixin_openid, ",
        "user_session_key, user_status, user_add_time, user_update_time, user_deleted",
        "from haogoumall_user",
        "where user_id = #{userId,jdbcType=INTEGER}"
    })

    /**@Results注解
     *      属性：
     *          id作为当前@Results注解的唯一标识很好理解；
     *          value值为@Result数组(其中可以定义实体类属性与数据库字段对应的参数)
     */
    @Results(id = "userResult",
            value = {
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="user_username", property="userUsername", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_password", property="userPassword", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_gender", property="userGender", jdbcType=JdbcType.TINYINT),
        @Result(column="user_birthday", property="userBirthday", jdbcType=JdbcType.DATE),
        @Result(column="user_last_login_time", property="userLastLoginTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="user_last_login_ip", property="userLastLoginIp", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_level", property="userLevel", jdbcType=JdbcType.TINYINT),
        @Result(column="user_nickname", property="userNickname", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_mobile", property="userMobile", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_avatar", property="userAvatar", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_weixin_openid", property="userWeixinOpenid", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_session_key", property="userSessionKey", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_status", property="userStatus", jdbcType=JdbcType.TINYINT),
        @Result(column="user_add_time", property="userAddTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="user_update_time", property="userUpdateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="user_deleted", property="userDeleted", jdbcType=JdbcType.BIT)
    })
    HaogoumallUser selectByPrimaryKey(Integer userId);

    @UpdateProvider(type=HaogoumallUserSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(HaogoumallUser record);

    @Update({
        "update haogoumall_user",
        "set user_username = #{userUsername,jdbcType=VARCHAR},",
          "user_password = #{userPassword,jdbcType=VARCHAR},",
          "user_gender = #{userGender,jdbcType=TINYINT},",
          "user_birthday = #{userBirthday,jdbcType=DATE},",
          "user_last_login_time = #{userLastLoginTime,jdbcType=TIMESTAMP},",
          "user_last_login_ip = #{userLastLoginIp,jdbcType=VARCHAR},",
          "user_level = #{userLevel,jdbcType=TINYINT},",
          "user_nickname = #{userNickname,jdbcType=VARCHAR},",
          "user_mobile = #{userMobile,jdbcType=VARCHAR},",
          "user_avatar = #{userAvatar,jdbcType=VARCHAR},",
          "user_weixin_openid = #{userWeixinOpenid,jdbcType=VARCHAR},",
          "user_session_key = #{userSessionKey,jdbcType=VARCHAR},",
          "user_status = #{userStatus,jdbcType=TINYINT},",
          "user_add_time = #{userAddTime,jdbcType=TIMESTAMP},",
          "user_update_time = #{userUpdateTime,jdbcType=TIMESTAMP},",
          "user_deleted = #{userDeleted,jdbcType=BIT}",
        "where user_id = #{userId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(HaogoumallUser record);


    @ResultMap("userResult")
    @Select("select * from haogoumall_user where user_username = #{userUsername}")
    /**验证用户名是否存在*/
    HaogoumallUser checkUserByUserName(HaogoumallUser user);


    /**@ResultMap中只有一个属性value(String类型的值)，用于指定要引用的@Results，value值为@Results的id值*/
    @ResultMap("userResult")
    @Select("select * from haogoumall_user where user_username = #{userUsername} and user_password = #{userPassword}")
    /**根据用户名和密码查询用户是否存在*/
    HaogoumallUser checkUserByUserNameAndPassword(HaogoumallUser user);


    @ResultMap("userResult")
    @Update("update haogoumall_user set user_last_login_time = #{userLastLoginTime},user_last_login_ip = #{userLastLoginIp} where user_username = #{userUsername}")
    /**更新用户的最后一次登录时间和Ip地址*/
    int updateUserLastLoginTimeAndUserLastLoginIp(HaogoumallUser user);


    @ResultMap("userResult")
    @Insert("insert into haogoumall_user(user_username,user_password,user_nickname,user_mobile,user_gender,user_avatar,user_add_time,user_update_time)values(#{userUsername},#{userPassword},#{userNickname},#{userMobile},#{userGender},#{userAvatar},#{userAddTime},#{userUpdateTime})")
    /**添加一条用户信息*/
    int addUser(HaogoumallUser user);


    @ResultMap("userResult")
    @Select("select * from haogoumall_user where user_mobile = #{userMobile}")
    /**验证手机号是否存在*/
    HaogoumallUser checkMobile(HaogoumallUser user);
}